package leetcode;

/**
 * @Description 1734. 解码异或后的排列
 * @Author away
 * @Version V1.0.0
 * @Since 1.0
 * @Date 21.5.11
 */

public class T1734 {
    public static void main(String[] args) {
        T1734 t1734 = new T1734();
        int[] encoded = { 6,5,4,6};
        System.out.println(t1734.decode(encoded));
    }


    /**
     * 给你一个整数数组 perm ，它是前 n 个正整数的排列，且 n 是个 奇数 。
     * 它被加密成另一个长度为 n - 1 的整数数组 encoded ，满足 encoded[i] = perm[i] XOR perm[i + 1] 
     * <p>
     * total = perm[0]^ perm[1] ^ ...perm[n-1]
     * enc = encoded[1] ^ encode[3] ^ [n-2]
     * total * enc = perm[0]
     * <p>
     * <p>
     * 2 4 6
     * 4 1 5
     * 1 5 4
     * 5 3 6
     *
     * @param encoded
     * @return
     */
    public int[] decode(int[] encoded) {
        int n = encoded.length + 1;

        int total = 0;
        for (int i = 1; i <= n; i++) {
            total ^= i;
        }
        int enc = 0;
        for (int i = 1; i < n - 1; i += 2) {
            enc ^= encoded[i];
        }

        int[] perm = new int[n];
        perm[0] = total ^ enc;
        for (int i = 1; i < n; i++) {
            perm[i] = perm[i - 1] ^ encoded[i - 1];
        }
        return perm;
    }
}
